import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _isBoolean from "lodash/isBoolean";
// 加载微前端模块
import pathToRegexp from 'path-to-regexp';
import { getConfigStore } from "../../index";
import getMicroPublicUrl, { getExternalMicroPublicUrlList } from "../../getMicroPublicUrlPrefix";
import { tabListen } from "cpaas-front/lib/utils/menuTab";
import "./loadingMicroStyle.less";

var fetchJsonp = require('fetch-jsonp');

var FETCH_JSONP_TIMEOUT = +"".concat(process.env.FETCH_JSONP_TIMEOUT || 12000); // const delay = (timeout) =>
//   new Promise((resolve) => {
//     setTimeout(() => {
//       resolve();
//     }, timeout);
//   });

var bootstrap = function bootstrap() {
  var dvaApp = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.dvaApp;
  // 挂载框架api到全局对象上 ， 子模块通过 externals 依赖引入
  window.React = require('react');
  window.ReactDOM = require('react-dom');

  var Vue = require('vue');

  window.Vue = Vue.default || Vue;
  window.regeneratorRuntime = require('regenerator-runtime');
  window.cpaasIcons = require('cpaas-front/lib/components/Icons'); // cpaas-front/lib/components/Icons

  window.axios = require('axios');
  window.mobx = require('mobx');
  window.codemirror = require('codemirror');
  window.classnames = require('classnames');
  window.moment = require('moment');
  window.lodash = require('lodash'); // (window as any).ReactBeautifulDnd = require('react-beautiful-dnd');

  window.styledComponents = require('styled-components');
  window.cpaasBasicPageBaseRouter = require('cpaas-front/lib/routes');
  window.uedContainer = require('@cpaas-front-ui/cfg/lib/components/Container');
  window.cpaasIoutils = require('cpaas-front/lib/utils/iocUtils');
  window.cpaasFrontUiCfg = require('@cpaas-front-ui/cfg');
  window.cpaasMapCustomize = require('cpaas-front/lib/utils/customize');
  window.cpaasFormatterCollections = require('cpaas-front/lib/utils/intl/formatterCollections');
  window.cpaasMenuTab = require('cpaas-front/lib/utils/menuTab');
  window.cpaasNProgress = require('cpaas-front/lib/components/NProgress');
  window.cpaasLoadingBar = require('cpaas-front/lib/components/NProgress/LoadingBar');
  window.cpaasUi = require('antd/lib');
  window.dva = require('dva');
  window.reactIntlUniversal = require('react-intl-universal');
  window.dvaDynamic = require('dva/dynamic');
  window.dvaRouter = require('dva/router');
  window.choerodonUi = require('choerodon-ui/lib');
  window.choerodonUiPro = require('choerodon-ui/pro/lib');
  var LOCATION_CHANGE_ACTION_TYPE = '@@router/LOCATION_CHANGE';
  var NAMESPACE = 'microModule';
  var UPDATE_MICRO_STATUS = 'microModule/UPDATE_MICRO_STATUS';
  var UPDATE_STATE = 'microModule/UPDATE_MICRO_STATE';
  var START_LISTEN_PATHNAME = 'microModule/START_LISTEN_PATHNAME';

  var setMicroLoading = function setMicroLoading(show) {
    var container = document.querySelector('.cpaas-normal-content-container');

    if (!container) {
      container = document.querySelector('.page-container');

      if (!container) {
        return;
      }
    }

    if (show) {
      container.classList.add('loading-micro');
    } else {
      container.classList.remove('loading-micro');
    }
  };

  var getMicroModuleRouters = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(opts) {
      var microModuleScriptMap, microModules, fetchData, allPromise, allMicroRoutes, mergeMicroRoutes, routerData;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              microModuleScriptMap = opts.microModuleScriptMap, microModules = opts.microModules;

              if (!(microModules.length === 0)) {
                _context2.next = 3;
                break;
              }

              return _context2.abrupt("return", {});

            case 3:
              fetchData = /*#__PURE__*/function () {
                var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(packageName) {
                  var packageInfo, indexAssets, basePath, scripts, styles, data;
                  return _regeneratorRuntime.wrap(function _callee$(_context) {
                    while (1) {
                      switch (_context.prev = _context.next) {
                        case 0:
                          packageInfo = microModuleScriptMap[packageName];

                          if (packageInfo) {
                            _context.next = 3;
                            break;
                          }

                          return _context.abrupt("return", {});

                        case 3:
                          indexAssets = packageInfo.indexAssets;
                          basePath = getMicroPublicUrl({
                            moduleName: packageName,
                            microModuleScriptMap: microModuleScriptMap
                          });

                          if (indexAssets) {
                            _context.next = 7;
                            break;
                          }

                          return _context.abrupt("return", {});

                        case 7:
                          scripts = indexAssets.filter(function (r) {
                            return r.endsWith('.js');
                          }).map(function (r) {
                            return "".concat(basePath).concat(r);
                          });
                          styles = indexAssets.filter(function (r) {
                            return r.endsWith('.css');
                          }).map(function (r) {
                            return "".concat(basePath).concat(r);
                          });
                          styles.forEach(function (styleUrl) {
                            var styleEle = document.createElement('link');
                            styleEle.setAttribute('rel', 'stylesheet');
                            styleEle.setAttribute('href', styleUrl);
                            document.head.insertBefore(styleEle, document.head.children[0]);
                          }); // 获取入口 html 模板及脚本加载器
                          // const entry = {
                          //   scripts,
                          // };
                          // const { execScripts } = await importEntry(entry);
                          // console.log('getMicroModuleRouters', appContent, execScripts, app);
                          // const data: any = await execScripts();

                          _context.prev = 10;
                          _context.next = 13;
                          return Promise.all(scripts.slice(1).map(function (s) {
                            return fetchJsonp("".concat(s), {
                              charset: 'UTF-8',
                              timeout: FETCH_JSONP_TIMEOUT
                            }).catch(function (e) {
                              console.error(e);
                            });
                          }));

                        case 13:
                          _context.next = 15;
                          return fetchJsonp("".concat(scripts[0]), {
                            timeout: FETCH_JSONP_TIMEOUT,
                            jsonpCallbackFunction: "cpaasJsonp_packages/".concat(packageName, "-main"),
                            charset: 'UTF-8'
                          }).then(function (r) {
                            return r.json();
                          });

                        case 15:
                          data = _context.sent;

                          if (data.getRouterData.beforeLoad) {
                            data.getRouterData.beforeLoad(dvaApp);
                          } // await delay(6000);


                          return _context.abrupt("return", data.getRouterData(dvaApp));

                        case 20:
                          _context.prev = 20;
                          _context.t0 = _context["catch"](10);
                          console.error(_context.t0);

                        case 23:
                          return _context.abrupt("return", {});

                        case 24:
                        case "end":
                          return _context.stop();
                      }
                    }
                  }, _callee, null, [[10, 20]]);
                }));

                return function fetchData(_x2) {
                  return _ref2.apply(this, arguments);
                };
              }();

              allPromise = microModules.map(function (packageName) {
                return fetchData(packageName);
              });
              _context2.next = 7;
              return Promise.all(allPromise);

            case 7:
              allMicroRoutes = _context2.sent;
              mergeMicroRoutes = allMicroRoutes.reduce(function (mergeObj, microRouteItem) {
                var retObj = _objectSpread({}, mergeObj);

                Object.keys(microRouteItem).forEach(function (path) {
                  var temp = null;

                  if (retObj[path]) {
                    temp = retObj[path];

                    if ((retObj[path].priority || 0) > (microRouteItem[path].priority || 0)) {
                      // if (
                      //   temp &&
                      //   (temp.index === false || microRouteItem[path].index === false)
                      // ) {
                      //   retObj[path].index = false;
                      // }
                      return;
                    }
                  } // if (retObj[path]) {
                  //   console.warn(
                  //     microRouteItem[path],
                  //     '  == (路由覆盖) =>  ',
                  //     retObj[path]
                  //   );
                  // }


                  retObj[path] = microRouteItem[path]; // if (temp && (temp.index === false || retObj[path].index === false)) {
                  //   retObj[path].index = false;
                  // }
                });
                return retObj;
              }, {});
              routerData = {}; // The route matches the menu

              Object.keys(mergeMicroRoutes).forEach(function (path) {
                if (routerData[path]) {
                  if ((routerData[path].priority || 0) > (mergeMicroRoutes[path].priority || 0)) {
                    return;
                  }
                } // Regular match item name
                // eg.  router /user/:id === /user/chen
                // @ts-ignore


                var pathRegexp = pathToRegexp(path);
                var router = mergeMicroRoutes[path];
                router = _objectSpread(_objectSpread({}, router), {}, {
                  name: router.name,
                  // tab 用到的数据
                  pathRegexp: pathRegexp,
                  title: router.title,
                  icon: router.icon,
                  closable: _isBoolean(router.closable) ? router.closable : true,
                  path: path,
                  // tab 用到的数据
                  authority: router.authority
                });
                routerData[path] = router;
              });
              return _context2.abrupt("return", routerData);

            case 12:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }));

    return function getMicroModuleRouters(_x) {
      return _ref.apply(this, arguments);
    };
  }();

  var getMicroModuleMapConfig = /*#__PURE__*/function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
      var basePath, microConfigMap, externalMicroPublicUrlList, _iterator, _step, _loop, microLoadInterceptor, allowLoadPackList, newMicroLoadedMap;

      return _regeneratorRuntime.wrap(function _callee4$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              basePath = getMicroPublicUrl();
              microConfigMap = {};
              _context5.prev = 2;
              _context5.next = 5;
              return fetch("".concat(basePath, "packages/microConfig.json?_t=").concat(new Date().getTime())).then(function (r) {
                return r.json();
              });

            case 5:
              microConfigMap = _context5.sent;
              _context5.next = 11;
              break;

            case 8:
              _context5.prev = 8;
              _context5.t0 = _context5["catch"](2);
              console.error(_context5.t0);

            case 11:
              _context5.next = 13;
              return Promise.all(Object.keys(microConfigMap).map( /*#__PURE__*/function () {
                var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(packageName) {
                  var packageInfo, publicUrl, extPackageInfo;
                  return _regeneratorRuntime.wrap(function _callee3$(_context3) {
                    while (1) {
                      switch (_context3.prev = _context3.next) {
                        case 0:
                          packageInfo = microConfigMap[packageName];

                          if (!packageInfo.external) {
                            _context3.next = 13;
                            break;
                          }

                          _context3.prev = 2;
                          publicUrl = packageInfo.publicUrl;
                          _context3.next = 6;
                          return fetchJsonp("".concat(publicUrl, "packages/").concat(packageName, "/package.js?_t=").concat(new Date().getTime()), {
                            jsonpCallbackFunction: "cpaasJsonp_packages/".concat(packageName, "-package"),
                            timeout: FETCH_JSONP_TIMEOUT,
                            charset: 'UTF-8'
                          }).then(function (r) {
                            return r.json();
                          });

                        case 6:
                          extPackageInfo = _context3.sent;
                          microConfigMap[packageName] = _objectSpread(_objectSpread({}, extPackageInfo), microConfigMap[packageName]);
                          _context3.next = 13;
                          break;

                        case 10:
                          _context3.prev = 10;
                          _context3.t0 = _context3["catch"](2);
                          console.error(_context3.t0);

                        case 13:
                        case "end":
                          return _context3.stop();
                      }
                    }
                  }, _callee3, null, [[2, 10]]);
                }));

                return function (_x3) {
                  return _ref4.apply(this, arguments);
                };
              }()));

            case 13:
              externalMicroPublicUrlList = getExternalMicroPublicUrlList();
              _iterator = _createForOfIteratorHelper(externalMicroPublicUrlList);
              _context5.prev = 15;
              _loop = /*#__PURE__*/_regeneratorRuntime.mark(function _loop() {
                var extBasePath, extMicroConfigMap;
                return _regeneratorRuntime.wrap(function _loop$(_context4) {
                  while (1) {
                    switch (_context4.prev = _context4.next) {
                      case 0:
                        extBasePath = _step.value;

                        if (!(extBasePath && extBasePath !== basePath)) {
                          _context4.next = 12;
                          break;
                        }

                        _context4.prev = 2;
                        _context4.next = 5;
                        return fetchJsonp("".concat(extBasePath, "packages/microConfig.js?_t=").concat(new Date().getTime()), {
                          jsonpCallbackFunction: "cpaasJsonp_packages",
                          timeout: FETCH_JSONP_TIMEOUT,
                          charset: 'UTF-8'
                        }).then(function (r) {
                          return r.json();
                        });

                      case 5:
                        extMicroConfigMap = _context4.sent;
                        Object.keys(extMicroConfigMap).forEach(function (packageName) {
                          if (!microConfigMap[packageName]) {
                            microConfigMap[packageName] = _objectSpread({
                              publicUrl: extBasePath
                            }, extMicroConfigMap[packageName]);
                          }
                        });
                        _context4.next = 12;
                        break;

                      case 9:
                        _context4.prev = 9;
                        _context4.t0 = _context4["catch"](2);
                        console.error(_context4.t0);

                      case 12:
                      case "end":
                        return _context4.stop();
                    }
                  }
                }, _loop, null, [[2, 9]]);
              });

              _iterator.s();

            case 18:
              if ((_step = _iterator.n()).done) {
                _context5.next = 22;
                break;
              }

              return _context5.delegateYield(_loop(), "t1", 20);

            case 20:
              _context5.next = 18;
              break;

            case 22:
              _context5.next = 27;
              break;

            case 24:
              _context5.prev = 24;
              _context5.t2 = _context5["catch"](15);

              _iterator.e(_context5.t2);

            case 27:
              _context5.prev = 27;

              _iterator.f();

              return _context5.finish(27);

            case 30:
              microLoadInterceptor = getConfigStore()['microLoadInterceptor'];

              if (!microLoadInterceptor) {
                _context5.next = 38;
                break;
              }

              _context5.next = 34;
              return microLoadInterceptor(Object.values(microConfigMap));

            case 34:
              allowLoadPackList = _context5.sent;
              newMicroLoadedMap = {};
              allowLoadPackList.forEach(function (packageInfo) {
                newMicroLoadedMap[packageInfo.name] = packageInfo;
              });
              microConfigMap = newMicroLoadedMap;

            case 38:
              return _context5.abrupt("return", microConfigMap);

            case 39:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee4, null, [[2, 8], [15, 24, 27, 30]]);
    }));

    return function getMicroModuleMapConfig() {
      return _ref3.apply(this, arguments);
    };
  }();

  var overloadRouterData = function overloadRouterData(routerData, addRouterData) {
    var newRouterData = _objectSpread({}, routerData);

    Object.keys(addRouterData).forEach(function (newRouterPath) {
      var temp = null;

      if (routerData[newRouterPath]) {
        temp = newRouterData[newRouterPath];

        if ((routerData[newRouterPath].priority || 0) > (addRouterData[newRouterPath].priority || 0)) {
          // if (
          //   temp &&
          //   (temp.index === false ||
          //     addRouterData[newRouterPath].index === false)
          // ) {
          //   newRouterData[newRouterPath].index = false;
          // }
          return;
        }
      } // 在覆盖前，被覆盖项若是父路由，则对父路由下的子路由清除loadIndex排序


      if (temp && temp.components instanceof Array && temp.components.length > 0) {
        temp.components.forEach(function (sub) {
          if (newRouterData[sub.path]) {
            delete newRouterData[sub.path].loadIndex;
          }
        });
      } // if (routerData[newRouterPath]) {
      //   console.warn(
      //     addRouterData[newRouterPath],
      //     '  == (路由覆盖) =>  ',
      //     routerData[newRouterPath]
      //   );
      // }


      newRouterData[newRouterPath] = addRouterData[newRouterPath]; // if (
      //   temp &&
      //   (temp.index === false || addRouterData[newRouterPath].index === false)
      // ) {
      //   newRouterData[newRouterPath].index = false;
      // }
    });
    Object.keys(newRouterData).forEach(function (newRouterPath) {
      var oldRoute = newRouterData[newRouterPath];

      if (oldRoute && oldRoute.components && oldRoute.components instanceof Array && oldRoute.components.length > 0) {
        oldRoute.loadIndex = oldRoute.loadIndex || 0;
        oldRoute.components.forEach(function (sub, index) {
          if (newRouterData[sub.path]) {
            newRouterData[sub.path].loadIndex = index + 1;
          }
        });
      }
    });
    return newRouterData; // cpaas-front 的坑， RouterData 必须对象的 key 必须有顺序要求。所以我要做一下 key 排序。
    // todo: 但是这不是一个稳定的解决方案。如果在其他浏览器环境中 object key 顺序自动随机。 还是会导致 bug;
    // return Object.keys(newRouterData)
    //   .sort((a, b) => a.length - b.length)
    //   .reduce((result: any, currentKey) => {
    //     return {
    //       ...result,
    //       [currentKey]: newRouterData[currentKey],
    //     };
    //   }, {});
  };

  var initialState = {
    loading: false,
    microStatusMap: {},
    microModuleScriptMap: null,
    waitLoadMicroModules: []
  };
  var namespace = NAMESPACE;

  var extraReducers = _defineProperty({}, namespace, function () {
    var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;

    var _ref5 = arguments.length > 1 ? arguments[1] : undefined,
        type = _ref5.type,
        payload = _ref5.payload;

    if (!payload) {
      return state;
    }

    var ret;

    switch (type) {
      case UPDATE_MICRO_STATUS:
        var waitLoadMicroModules = state.waitLoadMicroModules;

        var newMicroLoadedMap = _objectSpread({}, state.microStatusMap);

        var _payload = payload;

        _payload.microModules.forEach(function (packageName) {
          newMicroLoadedMap[packageName] = _payload.status;
        });

        var newWaitLoadMicroModules = waitLoadMicroModules;

        if (_payload.status === 'LOADED') {
          newWaitLoadMicroModules = waitLoadMicroModules.filter(function (packageName) {
            return !_payload.microModules.includes(packageName);
          });
        }

        ret = _objectSpread(_objectSpread({}, state), {}, {
          microStatusMap: newMicroLoadedMap,
          waitLoadMicroModules: newWaitLoadMicroModules,
          loading: _payload.status === 'PENDING'
        });
        break;

      case UPDATE_STATE:
        ret = _objectSpread(_objectSpread({}, state), payload);
        break;

      default:
        ret = state;
        break;
    }

    return ret;
  });

  var handlePathNameQueue = [];
  var allMicroLoaded = false;
  var handleLoadMicroing = false;

  var handleLoadMicro = /*#__PURE__*/function () {
    var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(_ref6) {
      var dispatch, getState, nextPathname, allState, _ref8, microModuleScriptMap, waitLoadMicroModules, pathname, loadPackage, microDataRoute, oldRouterData, newRouterData, currentPathname;

      return _regeneratorRuntime.wrap(function _callee5$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              dispatch = _ref6.dispatch, getState = _ref6.getState, nextPathname = _ref6.nextPathname;

              if (!handleLoadMicroing) {
                _context6.next = 5;
                break;
              }

              return _context6.abrupt("return");

            case 5:
              handleLoadMicroing = true;

            case 6:
              allState = getState();
              _ref8 = allState[namespace], microModuleScriptMap = _ref8.microModuleScriptMap, waitLoadMicroModules = _ref8.waitLoadMicroModules;

              if (!(microModuleScriptMap === null)) {
                _context6.next = 11;
                break;
              }

              handleLoadMicroing = false;
              return _context6.abrupt("return");

            case 11:
              if (!(waitLoadMicroModules.length === 0)) {
                _context6.next = 16;
                break;
              }

              allMicroLoaded = true;
              handleLoadMicroing = false;
              setTimeout(function () {
                allState = getState();
                var currentPathname = allState.routing.location.pathname;
                tabListen(currentPathname);
              }, 0);
              return _context6.abrupt("return");

            case 16:
              loadPackage = [];

              while (handlePathNameQueue.length) {
                pathname = handlePathNameQueue.pop();
                waitLoadMicroModules.forEach(function (packageName) {
                  var packageInfo = microModuleScriptMap[packageName];

                  if (!packageInfo || !packageInfo.registerRegex) {
                    return;
                  }

                  if (pathname && new RegExp(packageInfo.registerRegex).test(pathname) && !loadPackage.includes(packageName)) {
                    loadPackage.push(packageName);
                  }
                });
              }

              if (!(loadPackage.length > 0)) {
                _context6.next = 40;
                break;
              }

              _context6.prev = 19;
              dispatch({
                type: UPDATE_MICRO_STATUS,
                payload: {
                  microModules: loadPackage,
                  status: 'PENDING'
                }
              });
              dispatch({
                type: 'global/showLayoutPageLoading'
              });
              setMicroLoading(true);
              _context6.next = 25;
              return getMicroModuleRouters({
                microModules: loadPackage,
                microModuleScriptMap: microModuleScriptMap
              });

            case 25:
              microDataRoute = _context6.sent;
              oldRouterData = allState.global.routerData;
              newRouterData = overloadRouterData(oldRouterData, microDataRoute);
              dispatch({
                type: 'global/updateState',
                payload: {
                  routerData: newRouterData
                }
              });
              _context6.next = 34;
              break;

            case 31:
              _context6.prev = 31;
              _context6.t0 = _context6["catch"](19);
              console.error(_context6.t0);

            case 34:
              dispatch({
                type: UPDATE_MICRO_STATUS,
                payload: {
                  microModules: loadPackage,
                  status: 'LOADED'
                }
              });
              allState = getState();
              currentPathname = allState.routing.location.pathname;
              tabListen(currentPathname);
              dispatch({
                type: 'global/hideLayoutPageLoading'
              });
              setMicroLoading(false);

            case 40:
              handleLoadMicroing = false;

            case 41:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee5, null, [[19, 31]]);
    }));

    return function handleLoadMicro(_x4) {
      return _ref7.apply(this, arguments);
    };
  }();

  var onAction = function onAction(_ref9) {
    var dispatch = _ref9.dispatch,
        getState = _ref9.getState;
    return function (next) {
      return function (action) {
        switch (action.type) {
          case LOCATION_CHANGE_ACTION_TYPE:
            if (allMicroLoaded) {
              break;
            }

            var pathname = action.payload.pathname;

            if (!handlePathNameQueue.includes(pathname)) {
              handlePathNameQueue.push(pathname);
              handleLoadMicro({
                dispatch: dispatch,
                getState: getState,
                nextPathname: pathname
              });
            }

            break;

          case START_LISTEN_PATHNAME:
            if (handlePathNameQueue.length) {
              handleLoadMicro({
                dispatch: dispatch,
                getState: getState,
                nextPathname: pathname
              });
            }

            break;

          default:
            break;
        }

        return next(action);
      };
    };
  };

  var isInited = false;

  var onEffect = function onEffect(effect, _ref10, _, actionType) {
    var call = _ref10.call,
        put = _ref10.put,
        select = _ref10.select;

    if (actionType === 'global/baseLazyInit' || actionType === 'global/publicLazyInit' || actionType === 'global/pubLazyInit') {
      return /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
        var oldRouterData,
            currentRouterData,
            microModuleScriptMap,
            allPackages,
            isInitLoad,
            initMicroModules,
            waitLoadMicroModules,
            microDataRoute,
            _oldRouterData,
            newRouterData,
            currentPathname,
            _args7 = arguments;

        return _regeneratorRuntime.wrap(function _callee6$(_context7) {
          while (1) {
            switch (_context7.prev = _context7.next) {
              case 0:
                if (!isInited) {
                  _context7.next = 5;
                  break;
                }

                _context7.next = 3;
                return select(function (state) {
                  return state.global.routerData;
                });

              case 3:
                oldRouterData = _context7.sent;
                return _context7.abrupt("return");

              case 5:
                _context7.next = 7;
                return effect.apply(void 0, _args7);

              case 7:
                if (!isInited) {
                  _context7.next = 14;
                  break;
                }

                _context7.next = 10;
                return select(function (state) {
                  return state.global.routerData;
                });

              case 10:
                currentRouterData = _context7.sent;
                _context7.next = 13;
                return put({
                  type: 'global/updateState',
                  payload: {
                    routerData: _objectSpread(_objectSpread({}, currentRouterData), oldRouterData)
                  }
                });

              case 13:
                return _context7.abrupt("return");

              case 14:
                _context7.next = 16;
                return put({
                  type: 'global/showLayoutPageLoading'
                });

              case 16:
                setMicroLoading(true);
                allMicroLoaded = false;
                microModuleScriptMap = {};
                _context7.prev = 19;
                _context7.next = 22;
                return call(getMicroModuleMapConfig);

              case 22:
                microModuleScriptMap = _context7.sent;
                _context7.next = 28;
                break;

              case 25:
                _context7.prev = 25;
                _context7.t0 = _context7["catch"](19);
                console.error(_context7.t0);

              case 28:
                _context7.next = 30;
                return put({
                  type: UPDATE_STATE,
                  payload: {
                    microModuleScriptMap: microModuleScriptMap
                  }
                });

              case 30:
                allPackages = Object.keys(microModuleScriptMap);

                isInitLoad = function isInitLoad(packageInfo) {
                  return packageInfo.initLoad === true || packageInfo.initLoad === undefined && packageInfo.registerRegex === undefined;
                };

                initMicroModules = allPackages.filter(function (packageName) {
                  return isInitLoad(microModuleScriptMap[packageName]);
                });
                waitLoadMicroModules = allPackages.filter(function (packageName) {
                  return !isInitLoad(microModuleScriptMap[packageName]);
                });
                _context7.next = 36;
                return put({
                  type: UPDATE_STATE,
                  payload: {
                    initMicroModules: initMicroModules,
                    waitLoadMicroModules: waitLoadMicroModules
                  }
                });

              case 36:
                if (!(initMicroModules.length > 0)) {
                  _context7.next = 60;
                  break;
                }

                _context7.prev = 37;
                _context7.next = 40;
                return put({
                  type: UPDATE_MICRO_STATUS,
                  payload: {
                    microModules: initMicroModules,
                    status: 'PENDING'
                  }
                });

              case 40:
                _context7.next = 42;
                return call(getMicroModuleRouters, {
                  microModules: initMicroModules,
                  microModuleScriptMap: microModuleScriptMap
                });

              case 42:
                microDataRoute = _context7.sent;
                _context7.next = 45;
                return select(function (state) {
                  return state.global.routerData;
                });

              case 45:
                _oldRouterData = _context7.sent;
                newRouterData = overloadRouterData(_oldRouterData, microDataRoute);
                _context7.next = 49;
                return put({
                  type: 'global/updateState',
                  payload: {
                    routerData: newRouterData
                  }
                });

              case 49:
                _context7.next = 51;
                return select(function (state) {
                  return state.routing.location.pathname;
                });

              case 51:
                currentPathname = _context7.sent;
                tabListen(currentPathname);
                _context7.next = 58;
                break;

              case 55:
                _context7.prev = 55;
                _context7.t1 = _context7["catch"](37);
                console.log(_context7.t1);

              case 58:
                _context7.next = 60;
                return put({
                  type: UPDATE_MICRO_STATUS,
                  payload: {
                    microModules: initMicroModules,
                    status: 'LOADED'
                  }
                });

              case 60:
                _context7.next = 62;
                return put({
                  type: 'global/hideLayoutPageLoading'
                });

              case 62:
                setMicroLoading(false);
                _context7.next = 65;
                return put({
                  type: START_LISTEN_PATHNAME
                });

              case 65:
                isInited = true;

              case 66:
              case "end":
                return _context7.stop();
            }
          }
        }, _callee6, null, [[19, 25], [37, 55]]);
      });
    }

    return effect;
  };

  var hooks = {
    onAction: onAction,
    onEffect: onEffect,
    extraReducers: extraReducers
  };
  return hooks;
};

export default bootstrap;
